<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\tool\CommonTool;
use jianyan\excel\Excel;
use think\App;
use think\facade\Db;
use app\admin\model\Community;
use app\admin\model\Form2;
use app\admin\model\Form3;
use app\admin\model\Form5;

/**
 * @ControllerAnnotation(title="今日未回复记录")
 */
class Unanswered extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Form1();

        $this->assign('getSexList', $this->model->getSexList());

        $this->assign('getCheckResultList', $this->model->getCheckResultList());

        $this->assign('getCommunityList', Community::getCommunityList2(false));
    }

    /**
     * @NodeAnotation(title="返沙人员体温")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();

            // 社区数据筛选
            if (!$this->isSuper) {
                $where[] = ['community', '=', $this->user->community_id];
            }

            // 未解除状态
            $where[] = ['status', '=', 0];

            // 通知天数小于14天
            $where[] = ['count', '<', 28];

            $count = $this->model
                ->where($where)
                ->where('id_card', 'NOT IN', function ($query){
                    $query->name('form7')
                        ->whereDay('create_time')
                        ->group('id')
                        ->having('count(id) > 0')
                        ->field('id_card')
                        ->distinct(true)
                        ->select();
                })->count();

            $list = $this->model
                ->where($where)
                ->where('id_card', 'NOT IN', function ($query){
                    $query->name('form7')
                        ->whereDay('create_time')
                        ->group('id')
                        ->having('count(id) > 0')
                        ->field('id_card')
                        ->distinct(true)
                        ->select();
                })
                ->page($page, $limit)
                ->order($this->sort)
                ->select();

            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];

            // 下午17点才推送
            if (date('H') < 17) {
                $data = [
                    'code'  => 0,
                    'msg'   => '',
                    'count' => 0,
                    'data'  => [],
                ];
            }

            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="疫苗接种异常")
     */
    public function form2_index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();

            // 社区数据筛选
            if (!$this->isSuper) {
                $where[] = ['community', '=', $this->user->community_id];
            }

            //第一天为回复 或 第七天未回复
            $map1 = [
                ['count', '=', 0],
                ['status', '=', 0],
            ];
            $map2 = [
                ['count', '>', 0],
                ['status', '=', 0],
                ['sms_time', '<>', ''],
                ['sms_time', '<=', date('Y-m-d', time() - 7 * 24 * 60 * 60)],
            ];

            $count = Form2::where(function ($query) use($map1, $map2){
                    $query->whereOr([$map1, $map2]);
                })
                ->where($where)
                ->where('id_card', 'NOT IN', function ($query){
                    $query->name('form8')
                        ->whereDay('create_time')
                        ->group('id')
                        ->having('count(id) > 0')
                        ->field('id_card')
                        ->distinct(true)
                        ->select();
                })->count();

            $list = Form2::where(function ($query) use($map1, $map2){
                    $query->whereOr([$map1, $map2]);
                })
                ->where($where)
                ->where('id_card', 'NOT IN', function ($query){
                    $query->name('form8')
                        ->whereDay('create_time')
                        ->group('id')
                        ->having('count(id) > 0')
                        ->field('id_card')
                        ->distinct(true)
                        ->select();
                })
                ->page($page, $limit)
                ->order($this->sort)
                ->select();

            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];

            // 下午17点才推送
            if (date('H') < 17) {
                $data = [
                    'code'  => 0,
                    'msg'   => '',
                    'count' => 0,
                    'data'  => [],
                ];
            }

            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="购药人员体温")
     */
    public function form3_index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();

            // 社区数据筛选
            if (!$this->isSuper) {
                $where[] = ['community', '=', $this->user->community_id];
            }

            // 未解除状态
            $where[] = ['status', '=', 0];

            // 通知天数小于14天
            $where[] = ['count', '<', 28];

            $count = Form3::where($where)
                ->where('id_card', 'NOT IN', function ($query){
                    $query->name('form7')
                        ->whereDay('create_time')
                        ->group('id')
                        ->having('count(id) > 0')
                        ->field('id_card')
                        ->distinct(true)
                        ->select();
                })->count();

            $list = Form3::where($where)
                ->where('id_card', 'NOT IN', function ($query){
                    $query->name('form7')
                        ->whereDay('create_time')
                        ->group('id')
                        ->having('count(id) > 0')
                        ->field('id_card')
                        ->distinct(true)
                        ->select();
                })
                ->page($page, $limit)
                ->order($this->sort)
                ->select();

            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];

            // 下午17点才推送
            if (date('H') < 17) {
                $data = [
                    'code'  => 0,
                    'msg'   => '',
                    'count' => 0,
                    'data'  => [],
                ];
            }

            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="集中隔离体温")
     */
    public function form5_index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();

            // 社区数据筛选
            if (!$this->isSuper) {
                $where[] = ['community', '=', $this->user->community_id];
            }

            // 未解除状态
            $where[] = ['status', '=', 0];

            // 通知天数小于42天
            $where[] = ['count', '<', 42];

            $count = Form5::where($where)
                ->where('id_card', 'NOT IN', function ($query){
                    $query->name('form7')
                        ->whereDay('create_time')
                        ->group('id')
                        ->having('count(id) > 0')
                        ->field('id_card')
                        ->distinct(true)
                        ->select();
                })->count();

            $list = Form5::where($where)
                ->where('id_card', 'NOT IN', function ($query){
                    $query->name('form7')
                        ->whereDay('create_time')
                        ->group('id')
                        ->having('count(id) > 0')
                        ->field('id_card')
                        ->distinct(true)
                        ->select();
                })
                ->page($page, $limit)
                ->order($this->sort)
                ->select();

            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];

            // 下午17点才推送
            if (date('H') < 17) {
                $data = [
                    'code'  => 0,
                    'msg'   => '',
                    'count' => 0,
                    'data'  => [],
                ];
            }

            return json($data);
        }
        return $this->fetch();
    }
}